﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

/// <summary>
///Common 的摘要说明
/// </summary>
public class Common
{
	/// <summary>
    /// GridView排序方法
    /// </summary>
    /// <param name="gv"></param>
    /// <param name="e"></param>
    /// <returns></returns>
    public static string GetSortInfo(GridView gv, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        string sortDirection = string.Empty;
        foreach (DataControlField column in gv.Columns)
        {
            if (column.GetType() == typeof(BoundField))
            {
                ((BoundField)(column)).HtmlEncode = false;
            }
            if (column.SortExpression == e.SortExpression)
            {
                string str = column.HeaderText;
                if (str.IndexOf(CommonConst.ARROW_UP) + str.IndexOf(CommonConst.ARROW_DOWN) == -2)
                {
                    column.HeaderText = column.HeaderText + CommonConst.ARROW_DOWN;
                    sortDirection = " Desc";
                }
                else if (str.IndexOf(CommonConst.ARROW_UP) != -1)
                {
                    column.HeaderText = column.HeaderText.Replace(CommonConst.ARROW_UP, CommonConst.ARROW_DOWN);
                    sortDirection = " Desc";
                }
                else if (str.IndexOf(CommonConst.ARROW_DOWN) != -1)
                {
                    column.HeaderText = column.HeaderText.Replace(CommonConst.ARROW_DOWN, CommonConst.ARROW_UP);
                    sortDirection = " Asc";
                }
            }
            else
            {
                column.HeaderText = column.HeaderText.Replace(CommonConst.ARROW_UP, "").Replace(CommonConst.ARROW_DOWN, "").Trim();
            }
        }
        return sortExpression + sortDirection;
    }

    /// <summary>
    /// 验证上传图片是否非法
    /// </summary>
    /// <param name="postedFile"></param>
    /// <returns></returns>
    public static bool CheckImageFormat(HttpPostedFile postedFile)
    {
        if (postedFile.ContentType.IndexOf("image/", System.StringComparison.Ordinal) < 0)
            return false;
        if (postedFile.ContentLength < 50)
            return false;
        try
        {
            using (var img = System.Drawing.Image.FromStream(postedFile.InputStream))
            {
                if (img.Width * img.Height < 1)
                    return false;
                return img.RawFormat.Equals(ImageFormat.Jpeg) || img.RawFormat.Equals(ImageFormat.Png) ||
                      img.RawFormat.Equals(ImageFormat.Gif);

            }
        }
        catch (Exception)
        { 
          
            return false;
        }
    }

    public static  string GetCommentDelContent(int lanagerid)
    {
        return lanagerid == 2052 ? CommonConst.DeleteEventCommentCn : CommonConst.DeleteEventCommentEn;
    }

}